package com.oracle.log.lintener;

import com.alibaba.fastjson.JSONObject;
import com.oracle.log.lintener.service.LogsService;
import com.oracle.pojo.Logs;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
public class LogsAddConsumer implements MessageListener {

    @Autowired
    private LogsService logsService;

    @Override
    public void onMessage(Message message) {
        String json = null;
        try {
            json = new String(message.getBody(),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Logs logs = JSONObject.parseObject(json, Logs.class);
        System.out.println("添加日志");
        System.out.println(logs);
        this.logsService.addLogs(logs);
    }
}
